﻿using System;
using System.Collections.Generic;
using WEB.Core.Controls;
using Ext.Net;
using WEB.Core.Util;
using log4net;
using WEB.BLL.Services;
using WEB.BLL.Entities;
using WEB.BLL;

namespace WEB.APP.Modules.System
{
    public partial class Role : BasePage
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Role));
        public Role() : base(SCREENS.ROLE,false)
        {
        }

        protected override void Page_Load(object sender, EventArgs e)
        {
            AddListControlAdd(tbsAdd);
            this.WindowEditor.SaveClick += new EventHandler(WindowEditor_SaveClick);
            base.Page_Load(sender, e);
        }

        void WindowEditor_SaveClick(object sender, EventArgs e)
        {
            this.storeMain.DataBind();
        }
        protected void storeMain_OnRefreshData(object sender, StoreRefreshDataEventArgs e)
        {
            try
            {
                RoleEntity roleE = new RoleEntity();
                int start = Util.Obj2Int(e.Parameters["start"]);
                int endNum = Util.Obj2Int(e.Parameters["limit"]);
                endNum += start;
                roleE.Keyword = txtKeyword.Text;
                roleE.Start = start;
                roleE.EndNum = endNum;
                roleE.TotalRow = 0;
                RoleService roleS = RoleService.GetInstance();
                IList<RoleEntity> lstRole = roleS.Find(roleE);
                if (null == lstRole)
                    return;
                storeMain.DataSource = lstRole;
                storeMain.DataBind();
                e.Total = roleE.TotalRow;
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        public void btnAdd_Click(object sender, DirectEventArgs e)
        {
            WindowEditor.Show();
        }
        [DirectMethod]
        public void Edit(int id)
        {
            WindowEditor.Show(id, IsEdit);
        }
        [DirectMethod]
        public void Delete(int Id)
        {
            try
            {
                if (Id > 0)
                {
                    if (Id == 1 || Id == 0)
                    {
                        Msg.Informational("You can't delete this role.");
                        return;
                    }
                    RoleEntity roleE = new RoleEntity();
                    RoleService roleS = RoleService.GetInstance();
                    roleE.RoleId = Id;
                    roleS.Delete(roleE);
                    this.storeMain.DataBind();
                }
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        [DirectMethod]
        public void saveGrid_AfterEdit(int roleId, bool value)
        {
            try
            {
                RoleEntity roleE = new RoleEntity();
                RoleService roleS = RoleService.GetInstance();
                roleE.RoleId = roleId;
                roleE.IsActive = value;
                roleE.UpdatedDate = DateTime.Now;
                roleS.UpdateCheck(roleE);
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
    }
}
